package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"yun_image/config/api"
	"yun_image/entity"
	"yun_image/service"
)

func getHotsByWord(context *gin.Context) {
	hotName := context.PostForm("hotName")
	var hotService service.HotService
	hotWord := context.PostForm("hotWord")
	if hotWord == "" {
		context.String(http.StatusInternalServerError, "")
		return
	}
	// 必须为hots分配内存，如果只是var hots []entity.Hot，那么如果查询结果为空时，返回前端的是null值，而不是空数组[]

	if hotName == "weibo" {
		var hots = make([]entity.Hot, 0)
		err := hotService.GetWeiboByWord(&hots, hotWord)
		if err != nil {
			context.String(http.StatusInternalServerError, "")
			return
		}
		context.JSON(http.StatusOK, hots)
	} else if hotName == "baidu" {
		var hots = make([]entity.BaiduHot, 0)
		err := hotService.GetBaiduByWord(&hots, hotWord)
		if err != nil {
			context.String(http.StatusInternalServerError, "")
			return
		}
		context.JSON(http.StatusOK, hots)
	} else if hotName == "fenghuang" {
		var hots = make([]entity.FengHuangHot, 0)
		err := hotService.GetFengHuangByWord(&hots, hotWord)
		if err != nil {
			context.String(http.StatusInternalServerError, "")
			return
		}
		context.JSON(http.StatusOK, hots)
	} else if hotName == "bili" {
		var hots = make([]entity.BiliHot, 0)
		err := hotService.GetBiliByWord(&hots, hotWord)
		if err != nil {
			context.String(http.StatusInternalServerError, "")
			return
		}
		context.JSON(http.StatusOK, hots)
	} else {
		context.String(http.StatusInternalServerError, "")
	}

}

func GetHotsByTime(context *gin.Context) {
	hotName := context.PostForm("hotName")
	var hotService service.HotService
	firstTimeStickStr := context.PostForm("firstTimeStick")
	lastTimeStickStr := context.PostForm("lastTimeStick")
	firstTimeStick, err1 := strconv.ParseInt(firstTimeStickStr, 10, 64)
	lastTimeStick, err2 := strconv.ParseInt(lastTimeStickStr, 10, 64)
	if err1 != nil || err2 != nil {
		context.String(http.StatusInternalServerError, "")
		return
	}
	if hotName == "weibo" {
		var hots = make([]entity.Hot, 0)
		err := hotService.GetWeiboByTime(&hots, firstTimeStick, lastTimeStick)
		if err != nil {
			context.String(http.StatusInternalServerError, "")
			return
		}
		context.JSON(http.StatusOK, hots)
	} else if hotName == "baidu" {
		var hots = make([]entity.BaiduHot, 0)
		err := hotService.GetBaiduByTime(&hots, firstTimeStick, lastTimeStick)
		if err != nil {
			context.String(http.StatusInternalServerError, "")
			return
		}
		context.JSON(http.StatusOK, hots)
	} else if hotName == "fenghuang" {
		var hots = make([]entity.FengHuangHot, 0)
		err := hotService.GetFengHuangByTime(&hots, firstTimeStick, lastTimeStick)
		if err != nil {
			context.String(http.StatusInternalServerError, "")
			return
		}
		context.JSON(http.StatusOK, hots)
	} else if hotName == "bili" {
		var hots = make([]entity.BiliHot, 0)
		err := hotService.GetBiliByTime(&hots, firstTimeStick, lastTimeStick)
		if err != nil {
			context.String(http.StatusInternalServerError, "")
			return
		}
		context.JSON(http.StatusOK, hots)
	} else {
		context.String(http.StatusInternalServerError, "")
	}
}

func startWeiboApi() {
	router.POST(api.GetHotsByWordPath, getHotsByWord)
	router.POST(api.GetHotsByTimePath, GetHotsByTime)
}
